package com.winit.openapi.ims.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.ims.spi.inventory.InventoryService;
import com.winit.ims.spi.inventory.command.CalcForAllocationCommand;
import com.winit.ims.spi.inventory.command.CalcReplenishmentForAcrossCommand;
import com.winit.ims.spi.inventory.command.PageByWareAndMerchandiseCodeCommand;
import com.winit.ims.spi.inventory.command.PageForAcrossStorehouseCommand;
import com.winit.ims.spi.inventory.command.PageForStorageWarehouseCommand;
import com.winit.ims.spi.inventory.command.PageOverseaInventoriesCommand;
import com.winit.ims.spi.inventory.command.QueryForAcrossCommand;
import com.winit.ims.spi.inventory.command.QueryInventory4RecommendedDataCommand;
import com.winit.ims.spi.inventory.command.QueryInventoryByMerchandiseCommand;
import com.winit.ims.spi.inventory.command.QueryInventoryByWareAndSernoCommand;
import com.winit.ims.spi.inventory.command.QueryReturnInventoryCommand;
import com.winit.ims.spi.inventory.command.pageInventoryDetailCommand;
import com.winit.ims.spi.inventory.vo.CalcForAllocationVo;
import com.winit.ims.spi.inventory.vo.CalcReplenishmentForAcrossVo;
import com.winit.ims.spi.inventory.vo.Page4RecommendedDataVo;
import com.winit.ims.spi.inventory.vo.PageByWareAndMerchandiseCodeVo;
import com.winit.ims.spi.inventory.vo.PageForAcrossStorehouseVo;
import com.winit.ims.spi.inventory.vo.PageForStorageWarehouseVo;
import com.winit.ims.spi.inventory.vo.PageInventoryDetailVo;
import com.winit.ims.spi.inventory.vo.PageInventoryVo;
import com.winit.ims.spi.inventory.vo.PageOverseaInventoryVo;
import com.winit.ims.spi.inventory.vo.QueryForAcrossVo;
import com.winit.ims.spi.inventory.vo.QueryInventoryByMerchandiseVo;
import com.winit.ims.spi.inventory.vo.QueryReturnInventoryVo;
import com.winit.ims.spi.inventory.vo.ValidateIsEnoughInventoryVo;
import com.winit.ims.spi.vo.PageVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.customer.QueryCompanylCommand;
import com.winit.ums.spi.vo.CustomerInfoVO;

@Controller
@RequestMapping(value = "/ims/inventory")
public class InventoryController extends BaseController {

    @Resource
    private InventoryService   inventoryService;

    /**
     * ums客户信息
     */
    @Resource
    private UmsCustomerService umsCustomerService;

    /**
     * 查询海外仓库存
     * 
     * @param requestMsg
     * @return
     */
    @RequestMapping(value = "/queryProductInventoryList4Page")
    public String queryProductInventoryList4Page(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        PageOverseaInventoriesCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageOverseaInventoriesCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());
        Page<PageOverseaInventoryVo> pageOverseaInventoryVos = inventoryService.pageOverseaInventories(command);

        boolean isOpc = false;
        String opc = command.getSource();
        if ("OPC".equals(opc)) {// 是否卖家网站
            isOpc = true;
        }

        if (pageOverseaInventoryVos != null) {
            List<Object> list = new ArrayList<Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            for (PageOverseaInventoryVo vo : pageOverseaInventoryVos.content) {
                Map<String, Object> map = new HashMap<String, Object>();// 存放每行数据
                map.put("productId", "");// 产品ID
                map.put("merchandiseCode", vo.getMerchandiseCode());// 产品编码
                map.put("specification", vo.getSpecification());// 规格
                map.put("name", vo.getMerchandiseName());// 中文名称
                map.put("eName", vo.getMerchandiseEnName());// 英文名称
                map.put("description", vo.getDescription());// 描述
                String isProhibitOutbound = vo.getIsProhibitOutbound();
                if (isProhibitOutbound.contains("Y") && isProhibitOutbound.contains("N")) {
                    isProhibitOutbound = "PART";
                } else if (isProhibitOutbound.contains("Y")) {
                    isProhibitOutbound = "Y";
                } else {
                    isProhibitOutbound = "N";
                }
                map.put("isprohibitoutbound", isProhibitOutbound);// 禁止出库
                map.put("prohibitUsableQty", vo.getProhibitUsableQty());// 禁止出库
                map.put("qtyHisIn", vo.getInboundQty()+vo.getInboundAllocationQty());// 历史入库
                if ("Y".equals(vo.getIsProhibitOutbound()) && isOpc == false) {
                    map.put("qtyAvailable", 0);// 可用库存
                } else {
                    map.put("qtyAvailable", vo.getUsableQty());// 可用库存
                }
                map.put("qtyOrdered", vo.getOnOrderQty() + vo.getOnOrderAllocationQty()  +  vo.getSwOnOrderQty() );// 在途库存
                map.put("qtyReserved",vo.getSwStoreQty()+vo.getWaitingOutboundQty() +  vo.getWaitingDestructionQty() +vo.getWaitingOutboundAllocationQty()+vo.getLostConfirmingQty()+vo.getFrozenQty());// 待出库 = 标准待出库+销毁待出库+调拨待出库+丢失确认中+冻结
                map.put("qtySw", vo.getSwQty());// 存储模式库存
                map.put("qtyHisOut", vo.getOutboundQty()+  vo.getOutboundAllocationQty() + (vo.getDestructionQty() ==null?0:vo.getDestructionQty()));// 历史销量
                map.put("qtyShareStorage",0);// 共享库存
                map.put("qtySellHisOut", 0);// 历史代销量
                map.put("isReturn", vo.getIsReturn());// 是否为退货商品
                if ("Y".equals(vo.getIsReturn())) {
                    // 退货商品维护成原SKU时
                    map.put("DOI", "");// 前日DOI
                    map.put("averageStockQty", "");// 30天内平均库存数
                    map.put("averageSalesQty", "");// 30天内平均销量
                } else {
                    map.put("DOI", null != vo.getDoi() ? vo.getDoi() : "");// 前日DOI
                    map.put("DOIAll", null != vo.getCountryDoi() ? vo.getCountryDoi() : "");// 前日DOI
                    map.put("averageStockQty", null != vo.getAverageStockQty() ? vo.getAverageStockQty() : "");// 30天内平均库存数
                    map.put("averageStockQty7", null != vo.getAverageStockQty7() ? vo.getAverageStockQty7() : "");// 7天内平均库存数
                    map.put("averageStockQty15", null != vo.getAverageStockQty15() ? vo.getAverageStockQty15() : "");// 15天内平均库存数
                    map.put("averageSalesQty", null != vo.getAverageSalesQty() ? vo.getAverageSalesQty() : "");// 30天内平均销量
                    map.put("averageSalesQty7", null != vo.getAverageSalesQty7() ? vo.getAverageSalesQty7() : "");// 7天内平均销量
                    map.put("averageSalesQty15", null != vo.getAverageSalesQty15() ? vo.getAverageSalesQty15() : "");// 15天内平均销量

                }

                list.add(map);
            }
            data.put("list", list);
            
            Map<String, Object> pageMap = getPageParams(pageOverseaInventoryVos);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    
    
    
    
    
    /**
     * 卖家网站库存查询列表显示库存详情
     * 
     * @param requestMsg
     * @return
     */
    @RequestMapping(value = "/queryProductInventoryDetail")
    public String queryProductInventoryDetail(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        pageInventoryDetailCommand command = JSON.parseObject(requestMsg.getData().toString(),
            pageInventoryDetailCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());
        Page<PageInventoryDetailVo> PageInventoryDetailVos = inventoryService.pageInventoryDetail(command);
 
        boolean isOpc = false;
        String opc = command.getSource();
        if ("OPC".equals(opc)) {// 是否卖家网站
            isOpc = true;
        }

        if (PageInventoryDetailVos != null) {
            List<Object> list = new ArrayList<Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            for (PageInventoryDetailVo vo : PageInventoryDetailVos.content) {
                Map<String, Object> map = new HashMap<String, Object>();// 存放每行数据
                map.put("warehouseCode", vo.getWarehouseCode());// 仓库编码
                map.put("warehouseName", vo.getWarehouseName());// 仓库名称
                String isProhibitOutbound = vo.getIsProhibitOutbound();
                if (isProhibitOutbound.contains("Y") && isProhibitOutbound.contains("N")) {
                    isProhibitOutbound = "PART";
                } else if (isProhibitOutbound.contains("Y")) {
                    isProhibitOutbound = "Y";
                } else {
                    isProhibitOutbound = "N";
                }
                map.put("isprohibitoutbound", isProhibitOutbound);// 禁止出库
                map.put("qtyHisIn", vo.getInboundQty()+vo.getInboundAllocationQty());// 历史入库
                if ("Y".equals(vo.getIsProhibitOutbound()) && isOpc == false) {
                    map.put("qtyAvailable", 0);// 可用库存
                } else {
                    map.put("qtyAvailable", vo.getUsableQty());// 可用库存
                }
                map.put("qtyOrdered", vo.getOnOrderQty()+vo.getOnOrderAllocationQty()+vo.getSwOnOrderQty());// 在途出库
                // 待出库 = 标准待发+销毁待发+调拨待发+丢失确认中+冻结
                map.put("qtyReserved",vo.getSwStoreQty()+ vo.getFrozenQty()+vo.getWaitingOutboundQty()+vo.getWaitingDestructionQty()+vo.getWaitingOutboundAllocationQty()+vo.getLostConfirmingQty());
                map.put("qtySw", vo.getSwQty());// 存储模式库存
                map.put("qtyHisOut", vo.getOutboundQty()+vo.getOutboundAllocationQty()+vo.getDestructionQty());// 历史销量 = 出库历史销量+调拨历史销量
                map.put("qtyShareStorage",0);// 共享库存
                map.put("qtySellHisOut", 0);// 历史代销量
                if ("Y".equals(vo.getIsMaintainMerchandise())) {
                    // 退货商品维护成原SKU时
                    map.put("DOI", "");// 前日DOI
                    map.put("averageSalesQty", "");// 30天内平均销量
                    map.put("averageStockQty", "");// 30天内平均库存数
                } else {
                    // 退货商品维护成原SKU时
                    map.put("DOI", vo.getDoi());// 前日DOI
                    map.put("averageSalesQty",vo.getAvgSaleQty());// 30天内平均销量
                    map.put("averageStockQty", vo.getAverageStockQty());// 30天内平均库存数
                }

                list.add(map);
            }
            data.put("list", list);
            responseMsg.setData(data);
        }
        
        return SUCCESS;
    }

     
    
    /**
     * 查询海外仓库存- 导出
     * 
     * @param requestMsg
     * @return
     */
    @RequestMapping(value = "/reportProductInventoryList")
    public String reportProductInventoryList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        PageOverseaInventoriesCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageOverseaInventoriesCommand.class);
        command.setCtx(CommandContext.getContext());
        PageVo pageVo =command.getPageVo();
        
        if(null==pageVo){
              pageVo = new PageVo();
              pageVo.setPageNo(1);
        }
        if (pageVo.getPageSize() == null || pageVo.getPageSize() <= 0) {
            pageVo.setPageSize(20);
        }  
        
       
        
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());
        String fileUrl = inventoryService.reportProductInventoryList(command);

        

      
            Map<String, Object> data = new HashMap<String, Object>();
          
            data.put("fileUrl", fileUrl);
            responseMsg.setData(data);
      
        return SUCCESS;
    }

    

    /**
     * 根据商品条码模糊查询（可用数大于0）
     * 
     * @param requestMsg
     * @return
     */
    @RequestMapping(value = "/pageByWareAndMerchandiseCode")
    public String pageByWareAndMerchandiseCode(@ModelAttribute RequestMsg requestMsg,
                                               @ModelAttribute ResponseMsg responseMsg) {
        PageByWareAndMerchandiseCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageByWareAndMerchandiseCodeCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        PageVo pageVo = new PageVo();
        Object params = requestMsg.getData();
        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
            int pageNo = 1, pageSize = 10; // 分页参数
            if (ValidateUtil.isExistValue(json, "pageParams")) {
                JSONObject pageParam = json.getJSONObject("pageParams");
                if (ValidateUtil.isExistValue(pageParam, "pageNo")) {
                    pageNo = pageParam.getIntValue("pageNo") == 0 ? 1 : pageParam.getIntValue("pageNo");

                }
                if (ValidateUtil.isExistValue(pageParam, "pageSize")) {
                    pageSize = pageParam.getIntValue("pageSize");
                }
            }
            pageVo.setPageNo(pageNo);
            pageVo.setPageSize(pageSize);
            command.setPageVo(pageVo);
        } else {
            throw new ApiException(ErrorCode.ILLEGAL_JSON_STRING);
        }

        Page<PageByWareAndMerchandiseCodeVo> list = inventoryService.pageByWareAndMerchandiseCode(command);

        if (list != null) {
            List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
            for (PageByWareAndMerchandiseCodeVo vo : list.content) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("warehouseCode", vo.getWarehouseCode());
                map.put("customerCode", vo.getCustomerCode());
                map.put("customerName", vo.getCustomerName());
                map.put("merchandiseSerno", vo.getMerchandiseSerno());
                map.put("merchandiseName", vo.getMerchandiseName());
                map.put("merchandiseEnName", vo.getMerchandiseEnName());
                map.put("merchandiseCode", vo.getMerchandiseCode());
                map.put("usableQty", vo.getUsableQty());
                map.put("waitingOutboundQty", vo.getWaitingOutboundQty());
                map.put("frozenQty", vo.getFrozenQty());
                map.put("waitingDestructionQty", vo.getWaitingDestructionQty());
                map.put("inStockQty", vo.getInStockQty());
                map.put("specification", vo.getSpecification());
                map.put("formerMerchandiseSerno", vo.getFormerMerchandiseSerno());
                map.put("isMaintainMerchandise", vo.getIsMaintainMerchandise());
                datalist.add(map);
            }
            Map<String, Object> pageMap = getPageParams(list);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", datalist);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 根据商品编码，规格查询库存
     * 
     * @param requestMsg
     * @return
     */
    @RequestMapping(value = "/queryInventoryByMerchandise")
    public String queryInventoryByMerchandise(@ModelAttribute RequestMsg requestMsg,
                                              @ModelAttribute ResponseMsg responseMsg) {
        QueryInventoryByMerchandiseCommand command = JSON.parseObject(requestMsg.getData().toString(),
            QueryInventoryByMerchandiseCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());
        QueryInventoryByMerchandiseVo vo = inventoryService.queryInventoryByMerchandise(command);
        responseMsg.setData(vo);
        return SUCCESS;
    }

    /**
     * 当客户选择退货商品出库时，需将退货商品转换为具体出库商品 退货商品：退货商品维护为原sku的商品 ，具体数据为：商品编码（return）
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryReturnInventory")
    public String queryReturnInventory(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryReturnInventoryCommand command = new QueryReturnInventoryCommand();
        List<QueryReturnInventoryVo> list = new ArrayList<QueryReturnInventoryVo>();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            JSONArray array = json.getJSONArray("list");
            for (Object item : array) {
                JSONObject jsonObject = (JSONObject) item;
                QueryReturnInventoryVo queryReturnInventoryVo = new QueryReturnInventoryVo();
                queryReturnInventoryVo.setFormerMerchandiseSerno(jsonObject.getString("formerMerchandiseSerno"));
                queryReturnInventoryVo.setQtyDelivery(jsonObject.getInteger("qtyDelivery"));
                queryReturnInventoryVo.setWarehouseCode(jsonObject.getString("warehouseCode"));
                list.add(queryReturnInventoryVo);
            }
            command.setList(list);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        List<PageInventoryVo> returnlist = inventoryService.queryReturnInventory(command);
        responseMsg.setData(returnlist);
        return SUCCESS;
    }

    /**
     * 根据仓库编码与原商品条码查询退货商品可用库存 退货商品：退货商品维护为原sku的商品 ，具体数据为：商品编码（return）
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryInventoryByWareAndFormerSerno")
    public String queryInventoryByWareAndFormerSerno(@ModelAttribute RequestMsg requestMsg,
                                                     @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryReturnInventoryCommand command = new QueryReturnInventoryCommand();
        List<QueryReturnInventoryVo> list = new ArrayList<QueryReturnInventoryVo>();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            JSONArray array = json.getJSONArray("list");
            for (Object item : array) {
                JSONObject jsonObject = (JSONObject) item;
                QueryReturnInventoryVo queryReturnInventoryVo = new QueryReturnInventoryVo();
                queryReturnInventoryVo.setFormerMerchandiseSerno(jsonObject.getString("formerMerchandiseSerno"));
                queryReturnInventoryVo.setWarehouseCode(jsonObject.getString("warehouseCode"));
                list.add(queryReturnInventoryVo);
            }
            command.setList(list);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        List<PageInventoryVo> returnlist = inventoryService.queryInventoryByWareAndFormerSerno(command);
        responseMsg.setData(returnlist);
        return SUCCESS;
    }

    /**
     * 跨仓发货库存列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/pageForStorageWarehouse")
    public String pageForStorageWarehouse(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PageForStorageWarehouseCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageForStorageWarehouseCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        Page<PageForStorageWarehouseVo> pageVo = inventoryService.pageForStorageWarehouse(command);
        if (pageVo != null) {
            Map<String, Object> data = this.getPageParams(pageVo);
            data.put("list", pageVo.getContent());
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 跨仓发货库存列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/pageForAcrossStorehouse")
    public String pageForAcrossStorehouse(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PageForAcrossStorehouseCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageForAcrossStorehouseCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        Page<PageForAcrossStorehouseVo> pageVo = inventoryService.pageForAcrossStorehouse(command);
        if (pageVo != null) {
            Map<String, Object> data = this.getPageParams(pageVo);
            data.put("list", pageVo.getContent());
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 查询推荐计划
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryInventory4RecommendedData")
    public String queryInventory4RecommendedData(@ModelAttribute RequestMsg requestMsg,
                                                 @ModelAttribute ResponseMsg responseMsg) {
        QueryInventory4RecommendedDataCommand command = JSON.parseObject(requestMsg.getData().toString(),
            QueryInventory4RecommendedDataCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        Page<Page4RecommendedDataVo> pageVo = inventoryService.queryInventory4RecommendedData(command);
        if (pageVo != null) {
            Map<String, Object> data = this.getPageParams(pageVo);
            data.put("list", pageVo.getContent());
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 补货第二步，根据推荐数据全部下单
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryInventory4Recomm")
    public String queryInventory4Recomm(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PageForStorageWarehouseCommand command = JSON.parseObject(requestMsg.getData().toString(),
            PageForStorageWarehouseCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        List<PageForStorageWarehouseVo> pageVo = inventoryService.queryForStorageWarehouseByRecommended(command);
        if (pageVo != null) {
            responseMsg.setData(pageVo);
        }
        return SUCCESS;
    }

    /**
     * 查询跨仓发货库存
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryForAcross")
    public String queryForAcross(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryForAcrossCommand command = JSON.parseObject(requestMsg.getData().toString(), QueryForAcrossCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());

        List<QueryForAcrossVo> returnlist = inventoryService.queryForAcross(command);
        responseMsg.setData(returnlist);
        return SUCCESS;
    }

    /**
     * 计算补货量，跨仓
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/calcReplenishmentForAcross")
    public String calcReplenishmentForAcross(@ModelAttribute RequestMsg requestMsg,
                                             @ModelAttribute ResponseMsg responseMsg) {
        CalcReplenishmentForAcrossCommand command = JSON.parseObject(requestMsg.getData().toString(),
            CalcReplenishmentForAcrossCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());

        CalcReplenishmentForAcrossVo vo = inventoryService.calcReplenishmentForAcross(command);
        responseMsg.setData(vo);
        return SUCCESS;
    }

    /**
     * 计算补货量，调拔
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/calcReplenishmentForAllocation")
    public String calcReplenishmentForAllocation(@ModelAttribute RequestMsg requestMsg,
                                                 @ModelAttribute ResponseMsg responseMsg) {
        CalcForAllocationCommand command = JSON.parseObject(requestMsg.getData().toString(),
            CalcForAllocationCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());

        CalcForAllocationVo vo = inventoryService.calcReplenishmentForAllocation(command);
        responseMsg.setData(vo);
        return SUCCESS;
    }

    /**
     * 计算转存量，调拔
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/calcStoreForAllocation")
    public String calcStoreForAllocation(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CalcForAllocationCommand command = JSON.parseObject(requestMsg.getData().toString(),
            CalcForAllocationCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());

        CalcForAllocationVo vo = inventoryService.calcStoreForAllocation(command);
        responseMsg.setData(vo);
        return SUCCESS;
    }

    @RequestMapping(value = "/validateIsEnoughInventory")
    public String validateIsEnoughInventory(@ModelAttribute RequestMsg requestMsg,
                                            @ModelAttribute ResponseMsg responseMsg) {
        QueryInventoryByWareAndSernoCommand command = JSON.parseObject(requestMsg.getData().toString(),
            QueryInventoryByWareAndSernoCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        command.setIsAllowedSplitOrder("N");

        // 获取客户编码
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        command.setCustomerCode(customer.getCode());
        List<ValidateIsEnoughInventoryVo> vos = inventoryService.validateIsEnoughInventory(command);
        responseMsg.setData(vos);
        return SUCCESS;
    }

}
